﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Work.ClientEngine;
using System.Work.ProtoclBase;

namespace System.Work.ClientEngine.FPXH.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //192.168.0.5   192.168.0.1  127.0.0.1
            //SocketClient<BufferedPackageInfo> client = new SocketClient<BufferedPackageInfo>();
            SocketClientSync<BufferedPackageInfo> client = new SocketClientSync<BufferedPackageInfo>();
            client.LocalEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 9998);
            var filter = new Filter(new byte[] { 0x0D });
            client.Initialize(filter);
            client.Closed += Client_Closed;
            client.Connected += Client_Connected;
            client.Error += Client_Error;
            client.NewPackageReceived += Client_NewPackageReceived;
            client.ConnectAsync(new IPEndPoint(IPAddress.Parse("192.168.0.5"), 9094));
            Console.WriteLine("2");
            Console.ReadKey();
            client.Send(Encoding.ASCII.GetBytes("%01#RCSR511517\r"));

            Console.ReadKey();
            client.Close();
            Console.WriteLine("4");
            Console.ReadKey();
        }

        private static void Client_NewPackageReceived(object sender, PackageEventArgs<BufferedPackageInfo> e)
        {
            foreach (var package in e.Package.Data)
            {
                Console.WriteLine("Package:");
                //foreach (var d in package.Array)
                //{
                //    //if (d != 0x0d)
                //    //    Console.Write(string.Format("{0} ", d.ToString()));
                //}

                Console.WriteLine(Encoding.ASCII.GetString(package.Array));
                
            }
        }

        private static void Client_Error(object sender, ErrorEventArgs e)
        {
            Console.WriteLine("error");
        }

        private static void Client_Connected(object sender, EventArgs e)
        {
            Console.WriteLine("1");
        }

        private static void Client_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("3");
        }
    }
}
